package com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.repository;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.notice.FlowRuTaskNoticeChannelVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowRuTaskNoticeChannelRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowRuTaskNoticeChannel;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowRuTaskNoticeChannelExample;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.mapper.FlowRuTaskNoticeChannelDAO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author lilh
 * @date 2019-07-11 16:32
 */
@Service
public class FlowRuTaskNoticeChannelRepositoryImpl implements FlowRuTaskNoticeChannelRepository {

    @Autowired
    private FlowRuTaskNoticeChannelDAO flowRuTaskNoticeChannelDAO;

    @Override
    public int insertFlowRuTaskNoticeChannel(FlowRuTaskNoticeChannelVo entity) {
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        return flowRuTaskNoticeChannelDAO.insertSelective(convert(entity));
    }

    @Override
    public List<FlowRuTaskNoticeChannelVo> listFlowRuTaskNoticeChannel(List<Long> settingIds, String taskId) {
        FlowRuTaskNoticeChannelExample query = new FlowRuTaskNoticeChannelExample();
        query.createCriteria().andRuTimeoutSettingIdIn(settingIds).andTaskIdEqualTo(taskId);
        return listFlowRuTaskNoticeChannel(query).stream().map(this::convert).collect(Collectors.toList());
    }

    public List<FlowRuTaskNoticeChannel> listFlowRuTaskNoticeChannel(FlowRuTaskNoticeChannelExample query) {
        return flowRuTaskNoticeChannelDAO.selectByExample(query);
    }

    private FlowRuTaskNoticeChannel convert(FlowRuTaskNoticeChannelVo vo) {
        if (Objects.isNull(vo)) {
            return null;
        }

        FlowRuTaskNoticeChannel result = new FlowRuTaskNoticeChannel();
        BeanUtils.copyProperties(vo, result);
        return result;
    }

    private FlowRuTaskNoticeChannelVo convert(FlowRuTaskNoticeChannel entity) {
        if (Objects.isNull(entity)) {
            return null;
        }

        FlowRuTaskNoticeChannelVo result = new FlowRuTaskNoticeChannelVo();
        BeanUtils.copyProperties(entity, result);
        return result;
    }

}
